package com.ihr360.payroll.service.payroll.convertor;

import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.config.SalaryFieldEntity;
import com.ihr360.payroll.config.SalaryFieldTypeEntity;
import com.ihr360.payroll.config.SalaryFieldValueEntity;
import com.ihr360.payroll.config.SalaryTaxTypeEntity;
import com.ihr360.payroll.config.system.SystemSalaryFieldEntity;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryFieldType;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryTaxType;
import com.ihr360.payroll.model.dto.salary.embeddable.SalaryFieldValue;
import com.ihr360.payroll.model.dto.salary.meta.SalaryField;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SalaryFieldConvertor implements IConvertor<SalaryFieldEntity, SalaryField> {

    @Autowired
    SystemSalaryFieldConvertor systemSalaryFieldConvertor;

    @Override
    public SalaryField convertEntityToDto(SalaryFieldEntity entity) {
        return null;
    }

    @Override
    public SalaryFieldEntity convertDtoToEntity(SalaryField dto) {

        SalaryFieldEntity entity = new SalaryFieldEntity();
        BeanUtils.copyProperties(dto, entity);

        SalaryFieldValue fieldValue = dto.getValueMeta();
        if (fieldValue != null) {
            SalaryFieldValueEntity fieldValueEntity = new SalaryFieldValueEntity();
            BeanUtils.copyProperties(fieldValue, fieldValueEntity);
            entity.setValueMeta(fieldValueEntity);
        }

        SalaryFieldType fieldType = dto.getType();
        if (fieldType != null) {
            SalaryFieldTypeEntity fieldTypeEntity = new SalaryFieldTypeEntity();
            BeanUtils.copyProperties(fieldType, fieldTypeEntity);
            entity.setType(fieldTypeEntity);
        }

        SalaryTaxType taxType = dto.getTaxType();
        if (taxType != null) {
            SalaryTaxTypeEntity taxTypeEntity = new SalaryTaxTypeEntity();
            BeanUtils.copyProperties(taxType, taxTypeEntity);
            entity.setTaxType(taxTypeEntity);
        }


        SystemSalaryField systemSalaryField = dto.getMeta();
        SystemSalaryFieldEntity systemSalaryFieldEntity = systemSalaryFieldConvertor.convertDtoToEntity(systemSalaryField);
        entity.setMeta(systemSalaryFieldEntity);

        return entity;
    }
}